<?php

class Users_CommentsController extends Zend_Controller_Action {

    public function init() {
        $userValidation = new Validations_User();
        $valid = $userValidation->hasidentity();
        if (!$valid) {
            $this->_redirect('/user/login');
        }
    }

    public function indexAction() {
        // action body
    }

    public function newAction() {
        $validation = new Validations_User();
        $form = new Users_Form_Newcomment();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {                
                $values = $form->getValues();
                $comment = $values['comment'];
                $data = array(
                    'comment' => $comment,
                    'user_id' => $validation->getUserId(),
                    'date' => date('Y-m-d', date('U'))
                );
                $db = new Users_Model_Comments();
                $db->addNewComment($data);
            }
        }
        #getting all comments
        $userDb = new Users_Model_Users();
        $result = $userDb->getUserById($validation->getUserId());
        $this->view->allUsers = $result;
    }

    public function publicAction() {
        $id = $this->getinputidAction();
        $userDb = new Users_Model_Users();
        $result = $userDb->getUserById($id);
        $this->view->email = $result[0]['email'];
        $this->view->users = $result;
    }

    public function getinputidAction() {
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('Int')
        );
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        $id = $input->id;
        return $id;
    }

}

